package com.zhs.gccs.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hjq.shape.view.ShapeButton;
import com.zhs.gccs.R;
import com.zhs.gccs.adapter.GccsReportGridAdapter;
import com.zhs.gccs.adapter.GccsReportProblemGridAdapter;
import com.zhs.gccs.model.GccsOfProject;
import com.zhs.gccs.model.GccsProblem;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ScreenUtils;
import com.zhs.gccs.vo.GccsStatistics;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chengzf on 2024/7/22.
 */
public class GccsDataSearchDialog {

    public interface OnSelectedListener{
        void onSelected(String gccs,String problem);
    }

    public static class Builder extends BaseDialog.Builder<Builder> {
        private ShapeButton btn_reset;
        private ShapeButton btn_confirm;
        private GccsStatistics gccsStatistics;
        private RecyclerView tab_recycle_view;
        private List<GccsOfProject> list;
        List<GccsProblem> problems;
        private RecyclerView result_recycle_view;
        public Builder(Context context,GccsStatistics gccsStatistics,
                       String gccs,
                       String problem,
                       OnSelectedListener listener) {
            super(context);
            this.gccsStatistics = gccsStatistics;
            setContentView(R.layout.dialog_gccs_data_search_param);
            setAnimStyle(BaseDialog.ANIM_IOS);
//            setGravity(Gravity.CENTER);
            setGravity(Gravity.BOTTOM);
            setBackgroundDimEnabled(true);
            setCancelable(true);
            setCanceledOnTouchOutside(true);
            setWidth((int) (ScreenUtils.dpToPx(context, ScreenUtils.getScreenWidthDp(context)) * 1));
            setHeight((int) (ScreenUtils.getScreenHeightPx(context) * 0.6));
            tab_recycle_view = findViewById(R.id.tab_recycle_view);
            GccsReportGridAdapter mGridAdapter = new GccsReportGridAdapter();
            tab_recycle_view.setLayoutManager(new GridLayoutManager(context, 2));
            tab_recycle_view.setAdapter(mGridAdapter);
            if(gccsStatistics.getXmGccs()!=null){
                list = Stream.of(gccsStatistics.getXmGccs()).
                        filter(i -> !"84FC98D87113C32AE050007F0100549D".equals(i.getDbid()))
                        .toList();
                Stream.of(list).forEach(item->{
                            item.setSelected(item.getDbid().equals(gccs));
                });
                mGridAdapter.setNewData(list);
            }
            mGridAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    GccsOfProject gccsOfProject = list.get(position);
                    if(!gccsOfProject.isSelected()){
                        Stream.of(list).forEach(item->item.setSelected(false));
                        gccsOfProject.setSelected(true);
                    }else {
                        gccsOfProject.setSelected(false);
                    }
                    mGridAdapter.notifyDataSetChanged();
                }
            });
            result_recycle_view = findViewById(R.id.result_recycle_view);
            problems = new ArrayList<>();
            problems.add(new GccsProblem("合格","1","1".equals(problem)));
            problems.add(new GccsProblem("不合格","0","0".equals(problem)));
            GccsReportProblemGridAdapter problemGridAdapter = new GccsReportProblemGridAdapter();
            result_recycle_view.setLayoutManager(new GridLayoutManager(context, 2));
            result_recycle_view.setAdapter(problemGridAdapter);
            problemGridAdapter.setNewData(problems);
            problemGridAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    GccsProblem problem = problems.get(position);
                    if(!problem.isSelected()){
                        Stream.of(problems).forEach(item->item.setSelected(false));
                        problem.setSelected(true);
                    }else {
                        problem.setSelected(false);
                    }
                    problemGridAdapter.notifyDataSetChanged();
                }
            });
            btn_reset = findViewById(R.id.btn_reset);
            btn_reset.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    Stream.of(list).forEach(item->item.setSelected(false));
                    Stream.of(problems).forEach(item->item.setSelected(false));
                    dismiss();
                    if(listener!=null){
                        listener.onSelected("","");
                    }
                }
            });
            btn_confirm = findViewById(R.id.btn_confirm);
            btn_confirm.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    dismiss();
                    String gccs = Stream.of(list).filter(GccsOfProject::isSelected).map(GccsOfProject::getDbid).findFirstOrElse(null);
                    String problem = Stream.of(problems).filter(item -> item.isSelected()).map(GccsProblem::getValue).findFirstOrElse(null);
                    if(listener!=null){
                        listener.onSelected(gccs,problem);
                    }
                }
            });
        }

    }
}
